/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.bind.annotation.XmlTransient;
@XmlTransient
public class Parameter extends _Parameter
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String DEFAULT_SEPARATOR = " ";
	@XmlTransient
	protected Paragraph parent;
	@XmlTransient
	protected Program program;
	@XmlTransient
	protected String defaultValue;

	public Integer getGroup()
	{

		if (group == null)
		{
			if (parent == null)
			{
				return 0;
			} else
			{
				return parent.getGroup();
			}
		} else
		{
			return group;
		}
	}

	public Code getPrecond()
	{

		if (precond == null)
		{
			if (parent != null)
			{
				return parent.getPrecond();
			} else
			{
				return null;
			}
		} else
		{
			return precond;
		}
	}

	public Paragraph getParent()
	{
		return parent;
	}

	public void setParent(Paragraph parent)
	{
		this.parent = parent;
	}

	public Program getProgram()
	{
		return program;
	}

	public void setProgram(Program program)
	{
		this.program = program;
	}

	public String getDeaultValue()
	{
		return defaultValue;
	}
	
	public Object getDefaultValueAsObject()
	{
		return getValueAsObject(defaultValue);
	}
	
	public Object getValueAsObject(String value)
	{
		if (value==null)
			return null;
		switch(type)
		{
		case BooleanType:
		case IntegerType:
			return Integer.parseInt(value);
		case FloatType:
			return Double.parseDouble(value);
		default:
			return value;
		}
	}

	public void computeDefaultValue()
	{

		if (getVdef() == null)
		{
			defaultValue=null;
			return;
		}
		String vdef = getVdef().getValue();

		if (getType() == ParameterType.BooleanType)
		{

			if (vdef.equals("0") || vdef.equals("N") || vdef.equals("n") || vdef.equalsIgnoreCase("no"))
			{
				defaultValue = "0";
			} else
			{
				defaultValue = "1";
			}
		} else
		{
			defaultValue = vdef;
		}
	}
	
	
	public String getSeparator()
	{
		if (separator==null)
		{
			return Parameter.DEFAULT_SEPARATOR;
		} else
		{
			//a hack.
			//some program such as blastall using "\" \"" as separator.
			//it actually means " "
			if (separator.equals("\" \""))
			{
				return " ";
			} else
			{
				return separator;
			}
		}
	}
	
	public List<String> getDefaultAsList()
	{
		if (defaultValue!=null)
		{
			return Arrays.asList(defaultValue.split(getSeparator()));
		} else
		{
			return new ArrayList<String>();
		}
		
	}

}
